<template>
  <div class="requirementDetail-container">
    <div
      v-if="detailsData"
      class="app-title-container title-position"
    >
      <modules-title title-name="检验依据详情">
        <template slot="btn">
          <el-button
            plain
            @click="goBack"
          >返回</el-button>
          <template v-if="isHistory!==1">
            <el-button
              v-hasPermi="{v:['quality:all'],isF: detailsData.quality_standard_status==-3||detailsData.quality_standard_status==-1||detailsData.quality_standard_status==1||detailsData.quality_standard_status==3||detailsData.quality_standard_status==5}"
              plain
              @click="handleDelete"
            >删除</el-button>
            <el-button
              v-hasPermi="{v:['quality:all'], isF: detailsData.quality_standard_status==-3||detailsData.quality_standard_status==-2||detailsData.quality_standard_status==-1||detailsData.quality_standard_status==1||detailsData.quality_standard_status==3||detailsData.quality_standard_status==5}"
              type="primary"
              @click="editQuality"
            >编辑</el-button>
            <el-button
              v-if="detailsData.quality_standard_check&&detailsData.quality_standard_check.Cancelable==true&&detailsData.quality_standard_check.check_creator_id===userInfo.user.id"
              type="primary"
              @click="cancelOperation(detailsData.quality_standard_check.id)"
            >撤销</el-button>
            <el-button
              v-hasPermi="{v:['quality:all'], isF: detailsData.quality_standard_status==1}"
              type="primary"
              @click="submitApproval"
            >提交审批</el-button>
            <!-- <el-button
            v-hasPermi="{v:['quality:submit'],isF: detailsData.quality_standard_status==2}"
            type="primary"
            @click="auditOperation"
          >审批</el-button> -->
            <!-- <el-button
            v-hasPermi="{v:['quality:all'], isF: detailsData.quality_standard_status==4}"
            type="primary"
            @click="stopItem"
          >停用</el-button>
          <el-button
            v-hasPermi="{v:['quality:all'], isF: detailsData.quality_standard_status==3||detailsData.quality_standard_status==5}"
            type="primary"
            @click="startItem"
          >启用</el-button> -->
            <el-button
              v-hasPermi="{v:['quality:all'], isF: detailsData.quality_standard_status==4}"
              type="primary"
              @click="changeItem"
            >变更</el-button>
          </template>
        </template>
      </modules-title>
    </div>
    <div class="main-content-container">
      <!-- <div class="status-container">
        <status :status-data="statusData" />
      </div> -->
      <detail-content :details-data="detailsData" />
      <change-log
        v-if="detailsData&&detailsData.is_effect!==2"
        :project-id="qsid"
      />
      <option-log :project-id="qsid" />
    </div>
    <!-- 审批 -->
    <audit-dialog
      :id="qsid"
      ref="auditDialog"
      @auditSuccess="auditSuccess"
    />
  </div>
</template>

<script>
import {
  qualitySourceDetail,
  qualitySourceDelete,
  qualitySourceDetailByFlowId,
  changeStatus,
  submitAudit
} from '@/api/qualitySource'
export default {
  name: 'QualityDetail',
  components: {
    // status: () => import('./modules/status.vue'),
    DetailContent: () => import('./modules/detailContent.vue'),
    AuditDialog: () => import('../modules/auditDialog.vue'),
    OptionLog: () => import('./modules/optionLog.vue'),
    ChangeLog: () => import('./modules/changeLog.vue')
  },
  data() {
    return {
      userInfo: null,
      qsid: '', // 文件id
      flowId: '',
      statusData: { status: 0 },
      detailsData: null,
      isHistory: 0
    }
  },
  // 生命周期 - 创建完成（访问当前this实例）
  created() {
    this.userInfo = this.$store.getters.userInfo
    this.qsid = this.$route.query.qualityId || ''
    this.flowId = this.$route.query.f_id || ''
    this.isHistory = Number(this.$route.query.isHistory) || 0
    if (this.qsid) {
      this.getQualitySourceDetail(this.qsid)
    }
    if (this.flowId) {
      this.getQualitySourceDetailByFlowId(this.flowId)
    }
  },
  methods: {
    // 返回之后刷新列表
    refreshBack() {
      this.$route.params.refresh = true
      this.$router.back()
    },
    goBack() {
      this.refreshBack()
    },
    editQuality() {
      this.$router.push({
        name: 'createQuality',
        query: {
          qualitySourceId: this.qsid,
          types: 'edit'
        }
      })
    },
    changeItem() {
      this.$router.push({
        name: 'createQuality',
        query: {
          qualitySourceId: this.qsid,
          types: 'change'
        }
      })
    },
    // flowId获取文件详情
    getQualitySourceDetailByFlowId(id) {
      const datas = { id: id }
      qualitySourceDetailByFlowId(datas).then((res) => {
        if (res.code === 200) {
          this.detailsData = res.data
          this.statusData.status = this.detailsData.quality_standard_status
        }
      })
    },
    // 获取文件详情
    getQualitySourceDetail(id) {
      const datas = { id: id }
      qualitySourceDetail(datas).then((res) => {
        if (res.code === 200) {
          this.detailsData = res.data
          this.statusData.status = this.detailsData.quality_standard_status
        }
      })
    },
    submitApproval() {
      submitAudit({ id: this.qsid }).then((res) => {
        if (res.code === 200) {
          this.getQualitySourceDetail(this.qsid)
        } else {
          this.$message({
            type: 'error',
            message: res.msg,
            duration: 1500
          })
        }
      })
    },
    stopItem() {
      this.$confirm('是否确认停用该检验依据?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.changeStatusOperation(this.qsid, 5)
      })
    },
    startItem() {
      this.$confirm('是否确认启用该检验依据?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.changeStatusOperation(this.qsid, 4)
      })
    },
    changeStatusOperation(id, status) {
      const datas = { id: id, quality_standard_status: status }
      changeStatus(datas).then((res) => {
        if (res.code === 200) {
          this.getQualitySourceDetail(this.qsid)
        } else {
          this.$message({
            type: 'error',
            message: res.msg,
            duration: 1500
          })
        }
      })
    },
    auditOperation() {
      this.$refs.auditDialog.open()
    },
    auditSuccess() {
      this.getQualitySourceDetail(this.qsid)
    },
    handleDelete() {
      const that = this
      const datas = { id: this.qsid }
      this.$confirm('是否确认删除该文件?', '警告', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(function() {
          qualitySourceDelete(datas).then((res) => {
            if (res.code === 200) {
              that.$message({
                type: 'success',
                message: '删除成功',
                duration: 1500,
                onClose: () => {
                  that.refreshBack()
                }
              })
            } else {
              that.$message({
                type: 'error',
                message: res.msg,
                duration: 1500
              })
            }
          })
        })
        .catch(function() {})
    },
    // 撤销
    cancelOperation(id) {
      this.$alert('是否确认撤销？', '撤销', {
        type: 'warning',
        confirmButtonText: '确定',
        cancelButtonText: '取消'
      })
        .then(() => {
          const subdata = {
            flow_id: id,
            model_type: 'QualitySource'
          }
          this.$comFun
            .cancelFunc(subdata)
            .then(() => {
              this.$message({
                type: 'success',
                message: '已撤销'
              })
              this.getQualitySourceDetail(this.qsid)
            })
            .catch((err) => {
              this.$message.error(err)
            })
        })
        .catch((err) => {
          console.log(err)
        })
    }
  }
}
</script>
<style lang="scss" scoped>
/* @import url(); 引入css类 */
.requirementDetail-container {
  padding-top: 110px;
  box-sizing: border-box;
}
</style>
